package com.jjzy.realm;

import com.jjzy.pojo.ActiverUser;
import com.jjzy.pojo.User;
import com.jjzy.service.*;
import lombok.val;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import java.util.Collection;
import java.util.List;

/**
 * Author:   wj
 * Date:     2020/2/7 18:10
 * Description: 自定义的Realm
 */
public class MyRealm extends AuthorizingRealm {

    UserService userService = new UserServiceimpl();
    RoleService roleService = new RoleServiceImpl();
    PermissionService permissionService =new PermissionServiceImpl();


    /**
     * 设置凭证匹配器(密码的加密和解密)
     */
    public MyRealm(){
        HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
        //指定加密算法
        credentialsMatcher.setHashAlgorithmName("md5");
        //指定加密算法
        credentialsMatcher.setHashIterations(2);
        //凭证匹配器
        setCredentialsMatcher(credentialsMatcher);
    }

    /**
     * 做认证
     *
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        //将用户名取出
        String username = token.getPrincipal().toString();
        //将密码取出(无意义？)
        String password = token.getCredentials().toString();

        User user = userService.queryUserByName(username);
        if (null != user) {
            /**
             * 参数说明
             * 参数1:可以传到任意对象
             * 参数2:从数据库里面查询出来的密码
             * 参数3:盐
             * 将角色，权限存到 activerUser 中
             */

            List<String> roles = roleService.queryRoleByName(username);
            List<String> permissions = permissionService.queryPermissionByName(username);
            ActiverUser activerUser = new ActiverUser(user, roles, permissions);
            //盐，这里模仿从数据库查询
            ByteSource salt=ByteSource.Util.bytes("王");
            SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(activerUser, user.getPassword(),salt, this.getName());
            return info;
        } else {
            //shiro会抛 UnknowAccountException
            return null;
        }

    }

    /**
     * 做授权
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        //取出激活用户
        ActiverUser activerUser = (ActiverUser) principals.getPrimaryPrincipal();

        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //存入角色
        Collection<String> roles = activerUser.getRoles();

        if (!CollectionUtils.isEmpty(roles)) {
            info.addRoles(roles);
        }

        //存入权限
        Collection<String> permission = activerUser.getPermissions();

        if (!CollectionUtils.isEmpty(permission)) {
            info.addStringPermissions(permission);
        }
        return info;
    }


}
